﻿
//{{{ file documentation -------------------------------------------------------

/*!
 * @file     textedit.cpp
 * @brief    dicodor text edition widget class implementation
 */

//}}} --------------------------------------------------------------------------
//{{{ includes / predeclarations -----------------------------------------------

// @(#) $Id: textedit.cpp 53 2012-05-23 13:56:41Z nsouchon-dico@altern.org $

#include "textedit.h"
#include "moc_textedit.cpp"

#include <QtGui/QAction>
#include <QtGui/QWidgetAction>
#include <QtGui/QGridLayout>
#include <QtGui/QHBoxLayout>

//}}} --------------------------------------------------------------------------
//{{{ TextEdit -----------------------------------------------------------------
//}}} --------------------------------------------------------------------------
//{{{ .TextEdit (QWidget * parent) ---------------------------------------------

TextEdit::TextEdit (QWidget * parent) :
    QTextEdit (parent)
{
    setAcceptRichText (false);
    createCharMenu ();
}

//}}} --------------------------------------------------------------------------
//{{{ .~TextEdit () ------------------------------------------------------------

TextEdit::~TextEdit ()
{
    delete m_charMenu;
    delete m_charWidget;
}

//}}} --------------------------------------------------------------------------
//{{{ .void .createCharMenu () -------------------------------------------------

void TextEdit::createCharMenu ()
{
    m_charMenu   = new QMenu (tr ("Insérer un caractère..."), this);
    m_charWidget = new QWidget (this);

    // a newly created QWidget never has a layout

    if (false)
    {
        if (m_charWidget->layout () != 0)
        {
            delete m_charWidget->layout ();
        }
    }

    QGridLayout * layout = new QGridLayout (m_charWidget);

    // as layout is created created with m_charWidget as parent, no ned set layout

    if (false)
    {
        m_charWidget->setLayout (layout);
    }

    // critères de sélection des caractères à afficher

    for (ushort index = 1 ; index <= 0xff ; index++)
    {
        QChar car (index);

        // as I never remember operator precedence, I change this

        if (true)
        {
            if (car.isLetterOrNumber ()) continue;
            if (! car.isPrint ()) continue;
            if (car.toLatin1 () == car) continue;
            if (car.isSpace ()) continue;

            QWidgetAction * act = new QWidgetAction (m_charMenu);
            act->setText (QString (car));
            m_charMenu->addAction (act);
        }
        else if (! car.isLetterOrNumber () 
                && car.isPrint ()
                && (car.toLatin1 () != car)
                && ! car.isSpace ())
        {
            QWidgetAction * act = new QWidgetAction (m_charMenu);
            act->setText (QString (car));
            m_charMenu->addAction (act);
        }
    }
}

//}}} --------------------------------------------------------------------------
//{{{ .void .contextMenuEvent (QContextMenuEvent * event) ----------------------

void TextEdit::contextMenuEvent (QContextMenuEvent * event)
{
    // mettre le curseur à l'endroit du clic

    setTextCursor (cursorForPosition (event->pos ()));

    QMenu * standardMenu = createStandardContextMenu ();
    standardMenu->addSeparator ();
    standardMenu->addMenu (m_charMenu);

    if (QAction * act = standardMenu->exec (event->globalPos ()))
    {
        QString car (act->text ());
        if (car.length () == 1)
        {
            insertPlainText (car);
        }
    }

    delete standardMenu;
}

//}}} --------------------------------------------------------------------------

